home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c10 / IOStreamDemo.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  6.0 KB  |  174 lines

  1. //: IOStreamDemo.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Typical IO Stream Configurations
  50. import java.io.*;
  51. import com.bruceeckel.tools.*;
  52.  
  53. public class IOStreamDemo {
  54.   public static void main(String[] args) {
  55.     try {
  56.       // 1. Buffered input file
  57.       DataInputStream in =
  58.         new DataInputStream(
  59.           new BufferedInputStream(
  60.             new FileInputStream(args[0])));
  61.       String s, s2 = new String();
  62.       while((s = in.readLine())!= null)
  63.         s2 += s + "\n";
  64.       in.close();
  65.  
  66.       // 2. Input from memory
  67.       StringBufferInputStream in2 =
  68.           new StringBufferInputStream(s2);
  69.       int c;
  70.       while((c = in2.read()) != -1)
  71.         System.out.print((char)c);
  72.  
  73.       // 3. Formatted memory input
  74.       try {
  75.         DataInputStream in3 =
  76.           new DataInputStream(
  77.             new StringBufferInputStream(s2));
  78.         while(true)
  79.           System.out.print((char)in3.readByte());
  80.       } catch(EOFException e) {
  81.         System.out.println(
  82.           "End of stream encountered");
  83.       }
  84.  
  85.       // 4. Line numbering & file output
  86.       try {
  87.         LineNumberInputStream li =
  88.           new LineNumberInputStream(
  89.             new StringBufferInputStream(s2));
  90.         DataInputStream in4 =
  91.           new DataInputStream(li);
  92.         PrintStream out1 =
  93.           new PrintStream(
  94.             new BufferedOutputStream(
  95.               new FileOutputStream(
  96.                 "IODemo.out")));
  97.         while((s = in4.readLine()) != null )
  98.           out1.println(
  99.             "Line " + li.getLineNumber() + s);
  100.         out1.close(); // finalize() not reliable!
  101.       } catch(EOFException e) {
  102.         System.out.println(
  103.           "End of stream encountered");
  104.       }
  105.  
  106.       // 5. Storing & recovering data
  107.       try {
  108.         DataOutputStream out2 =
  109.           new DataOutputStream(
  110.             new BufferedOutputStream(
  111.               new FileOutputStream("Data.txt")));
  112.         out2.writeBytes(
  113.           "Here's the value of pi: \n");
  114.         out2.writeDouble(3.14159);
  115.         out2.close();
  116.         DataInputStream in5 =
  117.           new DataInputStream(
  118.             new BufferedInputStream(
  119.               new FileInputStream("Data.txt")));
  120.         System.out.println(in5.readLine());
  121.         System.out.println(in5.readDouble());
  122.       } catch(EOFException e) {
  123.         System.out.println(
  124.           "End of stream encountered");
  125.       }
  126.  
  127.       // 6. Reading/writing random access files
  128.       RandomAccessFile rf =
  129.         new RandomAccessFile("rtest.dat", "rw");
  130.       for(int i = 0; i < 10; i++)
  131.         rf.writeDouble(i*1.414);
  132.       rf.close();
  133.  
  134.       rf =
  135.         new RandomAccessFile("rtest.dat", "rw");
  136.       rf.seek(5*8);
  137.       rf.writeDouble(47.0001);
  138.       rf.close();
  139.  
  140.       rf =
  141.         new RandomAccessFile("rtest.dat", "r");
  142.       for(int i = 0; i < 10; i++)
  143.         System.out.println(
  144.           "Value " + i + ": " +
  145.           rf.readDouble());
  146.       rf.close();
  147.  
  148.       // 7. File input shorthand
  149.       InFile in6 = new InFile(args[0]);
  150.       String s3 = new String();
  151.       System.out.println(
  152.         "First line in file: " +
  153.         in6.readLine());
  154.         in6.close();
  155.  
  156.       // 8. Formatted file output shorthand
  157.       PrintFile out3 = new PrintFile("Data2.txt");
  158.       out3.print("Test of PrintFile");
  159.       out3.close();
  160.  
  161.       // 9. Data file output shorthand
  162.       OutFile out4 = new OutFile("Data3.txt");
  163.       out4.writeBytes("Test of outDataFile\n\r");
  164.       out4.writeChars("Test of outDataFile\n\r");
  165.       out4.close();
  166.  
  167.     } catch(FileNotFoundException e) {
  168.       System.out.println(
  169.         "File Not Found:" + args[0]);
  170.     } catch(IOException e) {
  171.       System.out.println("IO Exception");
  172.     }
  173.   }
  174. } ///:~